
package com.onpositive.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


public class ObjectSerializer {
	
	
	public static void serialize( Serializable obj, String path )
	{
		File f = new File(path) ;
		serialize(obj, f);				
	}

	public static void serialize(Serializable obj, File f){
		
		try{
			FileOutputStream fos;
			fos = new FileOutputStream( f );		
			serialize(obj, fos);
		} catch (FileNotFoundException e) {
			System.out.println( "ObjectSerializer, file not found:" + f.getPath() ) ;
		}
	}
	
	public static void serialize(Serializable obj, FileOutputStream fos) {
		try{
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(obj);
			oos.flush();
			oos.close();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}	

	public static Object deserialize(String path) {
		
		File f = new File(path) ;
		return deserialize(f);
	}
	
	public static Object deserialize(File f) {
		
		try {
			FileInputStream fis = new FileInputStream(f);
			return deserialize(fis) ;
		} catch (FileNotFoundException e) {
			System.out.println( "ObjectSerializer, file not found:" + f.getPath() ) ;
		}
		return null ;
	}

	public static Object deserialize(FileInputStream fis) {
		try{
			ObjectInputStream oin = new ObjectInputStream(fis);
			Object obj = oin.readObject();
			oin.close() ;
			return obj ;
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null ;
	}
	
	
	
	

}
